// 创建一个邻接矩阵

class MGraph {
    /**
     * 创建一个
     * @param {*} nodeList 顶点集
     * @param {*} edgeList 边集
     * @param {*} type 1 无向图， 2 有向图
     */
    constructor (nodeList, edgeList, type) {
        this.vexs = [] 
        this.edges = []
        // 存储图中顶点信息
        for (let node of nodeList) {
            this.vexs.push(node)
        }
        // 初始化边的信息为 0
        for (let i = 0; i < nodeList.length; i++) {
            this.edges[i] = []
            for (let j = 0; j < nodeList.length; j++) {
                if (i === j) {
                    this.edges[i][j] = 0
                } else {
                    this.edges[i][j] = 100000 // 没有边相连
                }
            }
        }
        // 存储边的信息（分有向图和无向图）
        for (let edge of edgeList) {
            if(type === '1') {
                this.edges[edge.row][edge.col] = edge.wei
                this.edges[edge.col][edge.row] = edge.wei
            } else {
                this.edges[edge.row][edge.col] = edge.wei
            }
        } 
    }
}

// const g = new MGraph(['A', 'B', 'C', 'D'], [{row: 0, col: 1, wei: 10}, {row: 0, col: 3, wei: 20}, {row: 1, col: 2, wei: 40}, {row: 2, col: 3, wei: 50}], '1')
// console.log(JSON.stringify(g))

module.exports = MGraph